Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Highlight Rust String interpolation macros #12768

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

nik-rev
Copy link
Contributor

@nik-rev nik-rev commented Feb 4, 2025

Hi, so I made a tree sitter grammar for rust format_args! which improves the Helix Rust experience, first of all by making it look prettier and easier to tell text apart from actual syntax

parser repo: tree-sitter-rustfmt: https://github.com/nik-rev/tree-sitter-rustfmt

Here's how it looks like:

Before

Image

After

Image

Notes

At the moment the PR injects this language into every string. What should be done instead is that it's injected into every string within a macro's arguments. I tried to do this, I'm not sure how to exactly. This was my best attempt, but it completely turned off syntax highlighting when I tried it:

(macro_invocation
  (string_content) @injection.content
  (#set! injection.language "rustfmt"))

Closes #5845

@@ -75,3 +75,7 @@
[(string_literal) (raw_string_literal)] @injection.content
)
(#set! injection.language "sql"))

; TODO: change this so it injects into all strings within a macro call
((string_content) @injection.content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to be smarter here and match on the macro name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I think just injecting it for stdlib and a few known crate macros (e.g. slog and tracing macros) would be a huge improvement already, and then you could always add more crates over time.

Copy link
Contributor Author

@nik-rev nik-rev Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following query should inject the language into the 1st argument, but it doesn't work due to a bug in tree-sitter which has been maybe fixed in v0.25

((macro_invocation
   macro:
     [
       (scoped_identifier
         name: (_) @_macro_name)
       (identifier) @_macro_name
     ]
   (token_tree . (string_literal) @injection.content))
 (#eq? @_macro_name "format_args")
 (#set! injection.language "rustfmt")
 (#set! injection.include-children))

For now, i made this PR such that it injects the rustfmt parser into every argument of these macros:

 (#any-of? @_macro_name
  ; std
  "format"
  "write"
  "writeln"
  "print"
  "println"
  "eprint"
  "eprintln"
  "format_args"
  ; log
  "crit"
  "error"
  "warn"
  "info"
  "debug"
  "trace"
  ; anyhow
  "anyhow"
  "bail"
  "ensure")

Once Helix updates to Tree-Sitter v0.25, we can make a new PR that will inject only into positional arguments

Copy link
Member

@the-mikedavis the-mikedavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want to wait for the new tree-sitter bindings and highlighter for this (#10286). I'm pretty sure the issue with the query you mentioned is that there isn't a way to control precedence of injections and the new highlighter will fix that.

name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree) @injection.content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than injecting into the whole token tree this should be targetting specifically (string_contents) nodes, and not setting injection.include-children. The rustfmt part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g. \u{20FF}), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#).

Copy link
Contributor Author

@nik-rev nik-rev Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than injecting into the whole token tree this should be targetting specifically (string_contents) nodes, and not setting injection.include-children. The rustfmt part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g. \u{20FF}), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#).

(escaped) is only responsible for detecting escaped {{ and }}, for example format!("{{ hello }}") will evaluate to "{ hello }". Since those aren't covered by rust's parser

Copy link
Contributor Author

@nik-rev nik-rev Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried setting string_contents using a variety of ways:

Before:

   (token_tree) @injection.content)

I removed the injection.include-children and then tried changing it to all of:

   (string_contents) @injection.content)
   (token_tree (string_contents) @injection.content))
   (token_tree (string_literal (string_contents) @injection.content)))
   (token_tree (string_contents)) @injection.content)

But in each case Rust syntax highlighting turns off. is it also related to #10286 ?

@RoloEdits
Copy link
Contributor

Encompassing the 2 feedback threads, would these changes/fixes also open up SQL being injected into query and query_as macros? I tried to get this to work before but didnt really know enough about TS to figure out what I was doing wrong.

@nik-rev
Copy link
Contributor Author

nik-rev commented Feb 4, 2025

Encompassing the 2 feedback threads, would these changes/fixes also open up SQL being injected into query and query_as macros? I tried to get this to work before but didnt really know enough about TS to figure out what I was doing wrong.

yes, injecting any other language into the macros should be easy to base off this PR (once it actually works, when the problems are fixed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Highlight for Rust string interpolation macros
5 participants